package com.qys.livesMall.event.listener;

import com.qys.livesMall.app.user.service.UserStatisticsDayLogService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.event.UserIncomeDayEvent;
import com.qys.livesMall.finance.model.dto.user.UserIncomeDayDTO;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
public class UserIncomeDayListener {

    private final UserStatisticsDayLogService userStatisticsDayLogService;

    private final UserStatisticsInfoService userStatisticsInfoService;

    @Order
    @Async
    @EventListener(UserIncomeDayEvent.class)
    public void saveUserRechargeInfo(UserIncomeDayEvent userIncomeDayEvent) throws InterruptedException {
        UserIncomeDayDTO userIncomeDayDTO= (UserIncomeDayDTO) userIncomeDayEvent.getSource();
        List<String> codes = AccountBusinessTypeEnum.getUserIncomeCode();
        if (codes.contains(userIncomeDayDTO.getCode())){
            boolean result=userStatisticsDayLogService.saveUserRechargeInfo(userIncomeDayDTO);
            if (!result){
                Thread.sleep(1000);
                userStatisticsDayLogService.saveUserRechargeInfo(userIncomeDayDTO);
            }
        }
        //主播收益
        List<String> anchorCodes=new ArrayList<>();
        anchorCodes.add(AccountBusinessTypeEnum.ORDER_PROFIT_ANCHOR_GET.getCode());
        anchorCodes.add(AccountBusinessTypeEnum.LIVE_GIFT_ANCHOR_GET.getCode());
        if (anchorCodes.contains(userIncomeDayDTO.getCode())){
            userStatisticsInfoService.updateAnchorIncome(userIncomeDayDTO);
        }
    }
}
